package cc.spider.web.model.http;


import cc.spider.model.TaskInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Request extends TaskInfo {
    private String url;

    private String method = HttpConstant.Method.GET;

    private List<IHeader> headers = new ArrayList<>();

    //private Map<String, String> headers = new HashMap<>();

    private Map<String, ICookie> cookies = new HashMap<>();

    private RequestBody requestBody = new RequestBody();

    private boolean isBinaryContent = false;

    private String contentCharset = "UTF-8";

    private int priority = 0;

    private int connectTimeout = -1;

    private String cookiePoolName;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.setUuKey(url);
        this.url = url;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public Map<String, ICookie> getCookies() {
        return cookies;
    }

    public void setCookies(Map<String, ICookie> cookies) {
        this.cookies = cookies;
    }

    public boolean isBinaryContent() {
        return isBinaryContent;
    }

    public void setBinaryContent(boolean binaryContent) {
        isBinaryContent = binaryContent;
    }

    public String getContentCharset() {
        return contentCharset;
    }

    public void setContentCharset(String contentCharset) {
        this.contentCharset = contentCharset;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public RequestBody getRequestBody() {
        return requestBody;
    }

    public void setRequestBody(RequestBody requestBody) {
        this.requestBody = requestBody;
    }

    public int getConnectTimeout() {
        return connectTimeout;
    }

    public void setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public String getCookiePoolName() {
        return cookiePoolName;
    }

    public void setCookiePoolName(String cookiePoolName) {
        this.cookiePoolName = cookiePoolName;
    }

    public void addCookie(Cookie cookie) {
        this.cookies.put(cookie.getName(), cookie);
    }

    public void addCookie(String name, String val) {
        this.cookies.put(name, new Cookie(name, val));
    }

    public List<IHeader> getHeaders() {
        return headers;
    }

    public List<IHeader> getHeaders(String headerName) {
        List<IHeader> headers = new ArrayList<>();
        for (IHeader header : this.headers) {
            if (header.getName().equals(headerName)) {
                headers.add(header);
            }
        }
        return headers;
    }

    public void setHeaders(List<IHeader> headers) {
        this.headers = headers;
    }

    public void addHeader(String name, String val) {
        IHeader header = new Header(name, val);
        this.headers.add(header);
    }
}
